import 'dart:convert' show json;

import 'sub_category.dart';

T asT<T>(dynamic value) {
  if (value is T) {
    return value;
  }

  return null;
}

class Category {
  Category({
    this.name,
    this.sub_category,
  });

  factory Category.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }

    final List<SubCategory> sub_category =
    jsonRes['sub_category'] is List ? <SubCategory>[] : null;
    if (sub_category != null) {
      for (final dynamic item in jsonRes['sub_category']) {
        if (item != null) {
          sub_category
              .add(SubCategory.fromJson(asT<Map<String, dynamic>>(item)));
        }
      }
    }
    return Category(
      name: asT<String>(jsonRes['name']),
      sub_category: sub_category,
    );
  }

  String name;
  List<SubCategory> sub_category;

  Map<String, dynamic> toJson() => <String, dynamic>{
    'name': name,
    'sub_category': sub_category,
  };
  @override
  String toString() {
    return json.encode(this);
  }
}